package com.wushi.lenovo.asia5b.app_ui.other.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseSimpleFragment;
import com.mvp.utils.DisplayUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2018/11/10.
 */
public class WelcomeFragment extends BaseSimpleFragment {

    @BindView(R.id.bt_welcome_start)
    TextView bt_welcome_start;
    @BindView(R.id.tv_welcome_skip)
    LinearLayout tv_welcome_skip;
    @BindView(R.id.rl_welcome_vessel)
    ConstraintLayout rl_welcome_vessel;
    @BindView(R.id.tv_welcome_donghua1)
    TextView tv_welcome_donghua1;
    @BindView(R.id.tv_welcome_donghua2)
    TextView tv_welcome_donghua2;
    private int position = 0;
    private TranslateAnimation animation1;
    private TranslateAnimation animation2;

    public static WelcomeFragment newInstance(Context context, int position) {

        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        WelcomeFragment welcomeFragment = (WelcomeFragment) Fragment.instantiate(context, WelcomeFragment.class.getName(), bundle);
        return welcomeFragment;

    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_welcome;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int width = DisplayUtils.getScreenWidthAndHight(getContext())[0];
        int diff = DisplayUtils.dip2px(getContext(), 30);
        animation1 = new TranslateAnimation(width, diff, 0, 0);
        animation2 = new TranslateAnimation(width, diff, 0, 0);
        animation1.setInterpolator(new OvershootInterpolator());
        animation2.setInterpolator(new OvershootInterpolator());
        animation1.setDuration(800);
        animation2.setDuration(800);
        position = getArguments().getInt("position", 0);
        tv_welcome_skip.setVisibility(position != 3 ? View.VISIBLE : View.INVISIBLE);
        if (position == 0) {
            rl_welcome_vessel.setBackgroundResource(R.drawable.img_welcome1);
            tv_welcome_donghua1.setText(R.string.welcome_yemian1_1);
            tv_welcome_donghua2.setText(R.string.welcome_yemian1_2);
        } else if (position == 1) {
            rl_welcome_vessel.setBackgroundResource(R.drawable.img_welcome2);
            tv_welcome_donghua1.setText(R.string.welcome_yemian2_1);
            tv_welcome_donghua2.setText(R.string.welcome_yemian2_2);
        } else if (position == 2) {
            rl_welcome_vessel.setBackgroundResource(R.drawable.img_welcome3);
            tv_welcome_donghua1.setText(R.string.welcome_yemian3_1);
            tv_welcome_donghua2.setText(R.string.welcome_yemian3_2);
        } else if (position == 3) {
            rl_welcome_vessel.setBackgroundResource(R.drawable.img_welcome4);
            tv_welcome_donghua1.setText(R.string.welcome_yemian4_1);
            tv_welcome_donghua2.setText(R.string.welcome_yemian4_2);
        }
        initEvent();
    }

    @Override
    protected void lazyFetchData() {

        tv_welcome_donghua1.setVisibility(View.VISIBLE);
        tv_welcome_donghua1.startAnimation(animation1);

        if (position == 3) {
            Disposable subscribe = Observable.timer(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> {
                        bt_welcome_start.setVisibility(View.VISIBLE);
                    });
            composite.add(subscribe);
        }
    }

    @Override
    protected void initEvent() {
        animation1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tv_welcome_donghua2.setVisibility(View.VISIBLE);
                tv_welcome_donghua2.startAnimation(animation2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    @OnClick({R.id.bt_welcome_start, R.id.tv_welcome_skip})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_welcome_skip:
            case R.id.bt_welcome_start:
                Timber.d("--->>>>>>>>---点击跳转主页");
                startActivity(new Intent(getContext(), MainActivity.class));
                getActivity().finish();
                break;

        }


    }
}
